package m.meiweitech.com.rocket.helper

import android.Manifest
import android.app.Activity
import android.bluetooth.BluetoothManager
import android.content.Context
import android.os.Build
import android.widget.Toast
import com.orhanobut.logger.Logger
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel
import m.meiweitech.com.rocket.R
import m.meiweitech.com.rocket.app.App
import m.meiweitech.com.rocket.entities.*
import m.meiweitech.com.rocket.ext.postObject
import m.meiweitech.com.rocket.util.MacCheckUtil
import m.meiweitech.com.rocket.util.SpUtil

/**
 * @author WEN
 * @Description:
 * @date 2022/1/1 16:53
 */
object BlueConnectStatusListener {

    private lateinit var binaryMessenger: BinaryMessenger

    private lateinit var connectStatusChannel:MethodChannel

    fun setBinaryMessenger(){
        this.binaryMessenger = App.flutterEngine.dartExecutor.binaryMessenger
        connectStatusChannel = MethodChannel(binaryMessenger, "connectStatus")
    }

    private val statusCallBack = mutableListOf<BlueConnectStatusCallBack>()

    private val flutterPageMark = mutableListOf<String>()

    private var deviceInfo:DeviceInfo? = null

    private var flutterDeviceInfo:FlutterDeviceInfo? = null

    fun isConnect():Boolean{
        return if(flutterDeviceInfo == null){
            false
        }else{
            flutterDeviceInfo!!.isConnect
        }
    }

    init {
        val adapter = (App.CONTEXT.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter
        if(adapter!=null){
            val device = adapter.getRemoteDevice(SpUtil.MMKV_mac)
            val isConnect = MacCheckUtil.isBtConDeviceByMac(SpUtil.MMKV_mac)
            deviceInfo = DeviceInfo(device,MachineInfoList[0].series,isConnect)
            flutterDeviceInfo = FlutterDeviceInfo(device,MachineInfoList[0].series,isConnect)
        }else{
           Logger.e("设备不支持蓝牙")
        }
    }

    fun registerListener(callBack: BlueConnectStatusCallBack, isReturnOnRegister:Boolean = false){
        statusCallBack.add(callBack)
//        if(isReturnOnRegister && deviceInfo!=null){
//            if(deviceInfo!!.isConnect){
//                callBack.onConnect(deviceInfo!!)
//            }else{
//                callBack.onDisconnect(deviceInfo!!)
//            }
//        }
    }

    fun unbindListener(callBack: BlueConnectStatusCallBack){
        statusCallBack.remove(callBack)
    }

    fun flutterRegisterListener(callBack: String, isReturnOnRegister:Boolean = true){
        flutterPageMark.add(callBack)

        if(isReturnOnRegister && flutterDeviceInfo!=null){
            if(flutterDeviceInfo!!.isConnect){
                Logger.i("onConnect  flutterRegisterListener，callBack = $callBack")
                connectStatusChannel.invokeMethod("onConnect",{"flutterDeviceInfo" to flutterDeviceInfo; "mark" to callBack}.toString())
            }else{
                Logger.i("onConnect  flutterRegisterListener，callBack = $callBack")
                connectStatusChannel.invokeMethod("onDisconnect",{"flutterDeviceInfo" to flutterDeviceInfo; "mark" to callBack}.toString())
            }
        }
    }

    fun flutterUnbindListener(callBack: String){
        flutterPageMark.remove(callBack)
    }

    fun notifyStatusAll(activity:Activity,device:FlutterDeviceInfo){

        val isConnect = if(flutterDeviceInfo!=null) flutterDeviceInfo!!.isConnect else false
        val mac = if(flutterDeviceInfo!=null) flutterDeviceInfo!!.device.address else SpUtil.MMKV_mac

        if(isConnect == device.isConnect && mac == device.device.address) return

        flutterDeviceInfo = device

//        statusCallBack.forEach {
//            if(deviceInfo!!.isConnect){
//                it.onConnect(deviceInfo!!)
//
//            }else{
//                it.onDisconnect(deviceInfo!!)
//            }
//        }

        flutterPageMark.forEach {
            if(flutterDeviceInfo!!.isConnect){
                Logger.i("onConnect  notifyStatusAll，callBack = $it")
                activity.runOnUiThread {
                    connectStatusChannel.invokeMethod("onConnect",{"flutterDeviceInfo" to flutterDeviceInfo; "mark" to it}.toString())
                }
            }else{
                Logger.i("onDisconnect  notifyStatusAll，callBack = $it")
                activity.runOnUiThread {
                    connectStatusChannel.invokeMethod("onDisconnect",{"flutterDeviceInfo" to flutterDeviceInfo; "mark" to it}.toString())
                }
            }
        }
    }

    fun notifyDisconnectStatusAll(){

        if(flutterDeviceInfo == null) return

        flutterDeviceInfo!!.isConnect = false

//        statusCallBack.forEach {
//            it.onDisconnect(flutterDeviceInfo!!)
//        }

        flutterPageMark.forEach {
            connectStatusChannel.invokeMethod("onDisconnect",{"flutterDeviceInfo" to flutterDeviceInfo; "mark" to it}.toString())
        }

    }

}